package com.fzy.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    private MD5Util() {
    }

    public static String md5(String str) {

        //创建MD5加密对象
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        // 进行加密
        md5.update(str.getBytes());
        //获取加密后的字节数组
        byte[] md5Bytes = md5.digest();
        StringBuilder sb = new StringBuilder();
        for (byte item : md5Bytes) {
            int temp = item & 0xFF;
            // 转化成十六进制不够两位，前面加零
            if (temp <= 0XF) {
                sb.append("0");
            }
            sb.append(temp);
        }
        return sb.toString();
    }

}
